package com.mobile.web.processFlow;

import com.mobile.service.dto.ProcessFlowDTO;
import com.mobile.service.dto.ProcessFlowQueryDTO;
import com.mobile.service.inter.ProcessFlowService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程表API接口
 */
@RestController
@RequestMapping("/processFlow")
@Tag(name = "流程管理接口", description = "包含流程的查询、新增、修改、删除等操作")
public class ProcessFlowController extends BaseController {

    @Autowired
    private ProcessFlowService processFlowService;

    @PostMapping("/addProcessFlow")
    //@RequiresPermissions("processFlowmana:add:1")
    @Operation(summary = "创建流程")
    @AutoFill(OperateType.INSERT)
    public Object addProcessFlow(@Valid @RequestBody ProcessFlowDTO dto) {
        Map<String, Object> map;
        try {
            map = processFlowService.addProcessFlow(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getProcessFlowListPage")
    //@RequiresPermissions("processFlowmana:read:1")
    @Operation(summary = "分页查询流程")
    public Object getProcessFlowListPage(@Validated ProcessFlowQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<ProcessFlowDTO> page = processFlowService.getProcessFlowListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
    @GetMapping(value = "/getAuditingWorkOrders")
    //@RequiresPermissions("processFlowmana:read:1")
    @Operation(summary = "分页查询待办工单")
    public Object getAuditingWorkOrders(@Validated ProcessFlowQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            queryDTO.setProcessorId(this.getUserCode());
            PageResult<ProcessFlowDTO> page = processFlowService.getAuditingWorkOrders(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getProcessFlowById")
    //@RequiresPermissions("processFlowmana:edit:1")
    @Operation(summary = "查询流程详情")
    public Object getProcessFlowById(
        @Parameter(description = "流程主键", required = true) 
        @RequestParam("processId") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            ProcessFlowDTO dto = processFlowService.getProcessFlowById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updateProcessFlow")
    //@RequiresPermissions("ProcessFlowmana:edit:1")
    @Operation(summary = "编辑流程")
    @AutoFill(OperateType.UPDATE)
    public Object updateProcessFlow(@Valid @RequestBody ProcessFlowDTO dto) {
        Map<String, Object> map;
        try {
            map = processFlowService.updateProcessFlow(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removeProcessFlow")
    //@RequiresPermissions("processFlowmana:delete:1")
    public Object removeProcessFlow(@RequestParam("processId") Integer id) {
        Map<String, Object> map;
        try {
            map = processFlowService.removeProcessFlow(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getProcessFlowList")
    //@RequiresPermissions("processFlowmana:read:1")
    @Operation(summary = "条件查询流程列表")
    public Object getProcessFlowList(@Validated ProcessFlowQueryDTO queryDTO) {
        
        Map<String, Object> map = new HashMap<>();
        try {
            List<ProcessFlowDTO> list = processFlowService.getProcessFlowList(queryDTO);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
    
    @GetMapping("/getTaskStatistics")
    @Operation(summary = "获取任务统计信息", description = "统计今日新增任务和超时任务")
    public Object getTaskStatistics(ProcessFlowQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            queryDTO.setProcessorId(this.getUserCode());
            Map<String, Object> statistics = processFlowService.getTaskStatistics(queryDTO);
            map.put("data", statistics);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
